#include "Misc.h"

CCNode* Misc::getChildByName(CCNode* pNode, const char* nodeName)
{
	std::string name(nodeName);
	if (pNode)
	{
		if (name.empty())
		{
			return NULL;
		}


		CCArray* pChildren = pNode->getChildren();
		if(pChildren && pChildren->count() > 0)
		{
			CCObject* child;
			CCARRAY_FOREACH(pChildren, child)
			{
				CCNode* pNode = (CCNode*) child;
				if(pNode && pNode->getName() == name)
					return pNode;
			}
			//if (bChild)
			{
				CCARRAY_FOREACH(pChildren, child)
				{
					CCNode* pNode = (CCNode*) child;
					if(pNode)
					{
						CCNode* pFind = getChildByName(pNode, nodeName);
						if (pFind)
							return pFind;
					}
				}
			}
		}
		return NULL;
	}
	else
	{
		CCLOG("%s getChildByName : %s is NULL", pNode->getName().c_str(), nodeName);
	}
	return NULL;
}

std::string Misc::getNodeName(CCNode* pNode)
{
	if (pNode)
	{
		return pNode->getName();
	}
	return "";
}

void Misc::setNodeName(CCNode* pNode, const char* name)
{
	if (pNode)
	{
		pNode->setName(name);
	}
}

void* Misc::getUserData(CCNode* pNode)
{
	return pNode->getUserData();
}

void Misc::log(const char* msg)
{
	CCLOG(msg);
}

std::string Misc::fullPathFile(const char* filename)
{
	return CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(filename);
}

CCRect Misc::getGlobalRect(cocos2d::CCNode *pNode)
{
	CCSize size = pNode->getContentSize();
	CCPoint globalPos1 = pNode->convertToWorldSpace(CCPoint(0, 0));
	CCPoint globalPos2 = pNode->convertToWorldSpace(CCPoint(0+size.width, 0+size.height));

	float minX = std::min<float>(globalPos1.x, globalPos2.x);
	float maxX = std::max<float>(globalPos1.x, globalPos2.x);
	float minY = std::min<float>(globalPos1.y, globalPos2.y);
	float maxY = std::max<float>(globalPos1.y, globalPos2.y);

	return CCRect(minX, minY, maxX - minX, maxY - minY);
}

CCRect Misc::getLocalRect(CCNode* pNode)
{
	CCSize size = pNode->getContentSize();
	return CCRect(0, 0, size.width, size.height);
}

const char* Misc::code_convert(char *from_charset,char *to_charset,const char *inbuf,int inlen,char *outbuf,int outlen)
{
	const char* orgBuf = outbuf; 
	iconv_t cd;
	int rc;
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
	char **pin = (char**)&inbuf;
#else
	const char **pin = &inbuf;
#endif
	char **pout = &outbuf;

	cd = iconv_open(to_charset,from_charset);
	if (cd==0) 
	{
		CCLog("Misc:code_convert : icon_open failed %s to %s", from_charset, to_charset);
		return inbuf;
	}

	memset(outbuf,0,outlen);
	if (iconv(cd, pin, (size_t *)&inlen, pout, (size_t *)&outlen) == -1)
	{
		CCLog("Misc:iconv : icon_open failed  %s to %s", from_charset, to_charset);
		iconv_close(cd);
		return inbuf;
	}
	iconv_close(cd);

	return orgBuf;
}

void Misc::splitString(const char* str, const char* split, std::vector<std::string>& outs)
{
	char* newStr = new char[strlen(str) + 1];
	strcpy(newStr, str);
	char* p = strtok((char*)newStr, split);
	if (p)
	{
		outs.push_back(p);
	}

	while(p)
	{
		p = strtok(NULL, split);
		if (p)
		{
			outs.push_back(p);
		}
	}
	delete []newStr;
}